package demo.xlj.yuyi.studentapp.common;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import demo.xlj.yuyi.studentapp.R;
import demo.xlj.yuyi.studentapp.base.BaseRxActivity;
import demo.xlj.yuyi.studentapp.bean.ShowHuoDongForAllBean;
import demo.xlj.yuyi.studentapp.retrofit.RetrofitClient;
import demo.xlj.yuyi.studentapp.rxjava.RxSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * 查看活动Activity--这里指的是最新的活动。
 */
public class LookAnnouncementActMore extends BaseRxActivity {

    @BindView(R.id.ImageView_back)
    ImageView mImageViewBack;
    @BindView(R.id.TextView_Title)
    TextView mTextViewTitle;
    @BindView(R.id.RecyclerView_look_announcement_act_more)
    RecyclerView mRecyclerViewLookAnnouncementActMore;



    private List<ShowHuoDongForAllBean.InfoBean> mList = new ArrayList<>();

    private BaseQuickAdapter<ShowHuoDongForAllBean.InfoBean, BaseViewHolder> baseQuickAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_announcement_act_more);
        ButterKnife.bind(this);
        mTextViewTitle.setText("最新活动");
        initRecyclerView();
        initData();

    }

    private void initData() {
        addRxDestroy(RetrofitClient.getInstance(LookAnnouncementActMore.this)
                .getApiService()
                .showHuoDong()
                .compose(RxSchedulers.<ShowHuoDongForAllBean>io_main())
                .subscribe(new Consumer<ShowHuoDongForAllBean>() {
                    @Override
                    public void accept(@NonNull ShowHuoDongForAllBean showHuoDongForAllBean) throws Exception {
                        if (showHuoDongForAllBean.getInfo().size() == 0) {
                            baseQuickAdapter.setEmptyView(R.layout.recycler_item_empty, mRecyclerViewLookAnnouncementActMore);
                        } else {
                            mList.addAll(showHuoDongForAllBean.getInfo());
                            baseQuickAdapter.addData(mList);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        baseQuickAdapter.setEmptyView(R.layout.recycler_item_empty, mRecyclerViewLookAnnouncementActMore);
                    }
                }));
    }

    @OnClick(R.id.ImageView_back)
    public void onViewClicked() {
        LookAnnouncementActMore.this.finish();
    }
    private void initRecyclerView() {
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(LookAnnouncementActMore.this);
        mRecyclerViewLookAnnouncementActMore.setLayoutManager(layoutManager);
        baseQuickAdapter = new BaseQuickAdapter<ShowHuoDongForAllBean.InfoBean, BaseViewHolder>(R.layout.huo_dong_info) {
            @Override
            protected void convert(BaseViewHolder helper, ShowHuoDongForAllBean.InfoBean item) {
                helper.setText(R.id.TextView_look_announcement_act_more_content, item.getContent());
                helper.setText(R.id.TextView_look_announcement_act_more_address, item.getAddr());
                helper.setText(R.id.TextView_look_announcement_act_more_time, item.getTime());
                helper.setText(R.id.TextView_look_announcement_act_more_undertak, (String) item.getUndertake());
            }
        };
        baseQuickAdapter.setEmptyView(R.layout.recycler_loading, mRecyclerViewLookAnnouncementActMore);
        mRecyclerViewLookAnnouncementActMore.setAdapter(baseQuickAdapter);
    }
}
